﻿using MovieTheater.Data;
using MovieTheater.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MovieTheater.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            MovieListViewModel model = new MovieListViewModel();
            using(var db = new MoviesDbContext())
            {
                model.Movies = db.Movies.Select(m => new MovieViewModel
                {
                    MovieTitle = m.Name,
                    PosterUrl = m.PosterUrl,
                    Rating = m.Rating,
                    UserName = m.User.UserName
                }).ToList();
            }

            model.AverageRating = model.Movies.Average(m => m.Rating);

            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}